package com.ce.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ce.core.AjaxResult;
import com.ce.entity.Storage;
import com.ce.service.StorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jyt
 * @since 2022-03-04
 */
@RestController
@RequestMapping("/storage")
public class StorageController {
    @Autowired
    StorageService service;

    /**
     * 根据仓库编号查询库存信息
     */
    @PostMapping("/getStorageByWareNo")
    public AjaxResult selectByWareNo(Storage storage) {
        /*System.out.println("storage:"+warehouseNo);
        Storage storage = new Storage();
        storage.setWarehouseNo(warehouseNo);*/
        Storage storage1 = service.selectByProductNoAndWarehouseNo(storage);
        //System.out.println(storage1.getWarehouseNo());
        return AjaxResult.success(storage1);
    }

    /**
     * 查询库存信息
     */
    @GetMapping("/getStorageList")
    public List<Storage> testSelect() {
        List<Storage> storages = service.getList();
        return storages;
    }

    /**
     * 库存调拨
     */
    @PostMapping("/adjust")
    public AjaxResult updateStorage(HttpServletRequest request) {
        Storage storage = new Storage();
        storage.setId(Integer.parseInt(request.getParameter("id")));
        storage.setWarehouseNoIn(request.getParameter("warehouseNoIn"));
        storage.setWarehouseNoOut(request.getParameter("warehouseNoOut"));
        storage.setStorageSum(Integer.parseInt(request.getParameter("storageSum")));
        storage.setProductNo(request.getParameter("productNo"));
        // 因为要根据商品编号和仓库编号查询库存，所以要set一下仓库编号（也就是转出仓编号）
        storage.setWarehouseNo(request.getParameter("warehouseNoOut"));
        // 库存调拨---先查询是否有该商品库存信息，TODO：如果没有则提示无库存、有则更新
        if (ObjectUtils.isNotEmpty(service.selectByProductNoAndWarehouseNo(storage))) {
            service.updateStorage(storage);
        } else {
            System.out.println("无库存");
            return AjaxResult.error(500,"无库存,请选择有库存的仓库");
        }
        return AjaxResult.success("库存调拨成功");
    }

}
